﻿using Avalonia.Collections;
using Avalonia.Platform.Storage;
using Digitalmes.Infrastructure.IO;
using ScottPlot.Avalonia;

namespace Digitalmes.AvaloniaUI.ViewModels;

/// <summary>
/// 自定义控件
/// </summary>
public sealed partial class LedDemoViewModel : ViewModelBase
{
    [ObservableProperty]
    private LedState _ledState1 = LedState.Good;

    [ObservableProperty]
    private string? _ledText = "LED";

    [ObservableProperty]
    private bool _ledAllowDrag;

    [ObservableProperty]
    private LedCanvasSource? _ledCanvasSource;

    [ObservableProperty]
    private LedCanvasAcceptPoint? _ledAcceptPoint;

    [ObservableProperty]
    private bool? _ledIsReset;

    [RelayCommand]
    private void ActiveLed(LedState state)
    {
        LedText = state.ToString();

        LedCanvasSource?.Points.ForEach(s => s.State = state);
    }

    [RelayCommand]
    private void ResetLed()
    {
        LedIsReset = !LedIsReset ?? true;
    }

    private int _sourceIndex = 0;

    [RelayCommand]
    private void ChangeLedSource()
    {
        LedCanvasSource = LedSources[++_sourceIndex % 2];
        PollingTask(LedCanvasSource.Name);
    }

    public LedDemoViewModel()
    {
        LedAllowDrag = true;
        LedCanvasSource = LedSources[_sourceIndex];

        PollingTask(LedCanvasSource.Name);
    }

    private void PollingTask(string sourceName)
    {
        string[] groups = ["A", "B"];
        foreach (var group in groups)
        {
            _ = Task.Run(async () =>
            {
                int count = LedCanvasSource!.Points.Where(s => s.Group == group).Count();
                int index = 0;

                Random rnd = new();
                while (LedCanvasSource.Name == sourceName)
                {
                    await Task.Delay(rnd.Next(2_000, 3_000)).ConfigureAwait(false);
                    if (LedCanvasSource.Name != sourceName)
                    {
                        break;
                    }

                    if (index > count)
                    {
                        index = 0;
                    }

                    LedAcceptPoint = new LedCanvasAcceptPoint { Group = group, Index = index++, IsGood = rnd.Next(7) % 2 == 0 };
                }
            });
        }
    }

    private static readonly LedCanvasSource[] LedSources =
    {
        new()
        {
            Name = "Source01",
            Width = 600,
            Height = 300,
            ImagePath = "Assets/Images/P01.png",
            Points =
            [
                new LedPoint() { Diameter = 36, Text = "A01", Group = "A", Index = 1, X = 60, Y = 40 },
                new LedPoint() { Diameter = 36, Text = "A02", Group = "A", Index = 2, X = 120, Y = 40 },
                new LedPoint() { Diameter = 36, Text = "A03", Group = "A", Index = 3, X = 180, Y = 40 },
                new LedPoint() { Diameter = 36, Text = "A04", Group = "A", Index = 4, X = 80, Y = 90 },
                new LedPoint() { Diameter = 36, Text = "A05", Group = "A", Index = 5, X = 160, Y = 90 },
                new LedPoint() { Diameter = 36, Text = "A06", Group = "A", Index = 6, X = 260, Y = 90 },

                new LedPoint() { Diameter = 36, Text = "B01", Group = "B", Index = 1, X = 60, Y = 180 },
                new LedPoint() { Diameter = 36, Text = "B02", Group = "B", Index = 2, X = 120, Y = 180 },
                new LedPoint() { Diameter = 36, Text = "B03", Group = "B", Index = 3, X = 180, Y = 180 },
                new LedPoint() { Diameter = 36, Text = "B04", Group = "B", Index = 4, X = 80, Y = 230 },
                new LedPoint() { Diameter = 36, Text = "B05", Group = "B", Index = 5, X = 160, Y = 230 },
                new LedPoint() { Diameter = 36, Text = "B06", Group = "B", Index = 6, X = 260, Y = 230 },
            ],
        },
        new()
        {
            Name = "Source02",
            Width = 500,
            Height = 260,
            ImagePath = "Assets/Images/P02.png",
            Points =
            [
                new LedPoint() { Diameter = 36, Text = "A01", Group = "A", Index = 1, X = 60, Y = 60 },
                new LedPoint() { Diameter = 36, Text = "A02", Group = "A", Index = 2, X = 120, Y = 60 },
                new LedPoint() { Diameter = 36, Text = "A03", Group = "A", Index = 3, X = 180, Y = 60 },
                new LedPoint() { Diameter = 36, Text = "A04", Group = "A", Index = 4, X = 80, Y = 110 },
                new LedPoint() { Diameter = 36, Text = "A05", Group = "A", Index = 5, X = 160, Y = 110 },
                new LedPoint() { Diameter = 36, Text = "A06", Group = "A", Index = 6, X = 260, Y = 110 },

                new LedPoint() { Diameter = 36, Text = "B01", Group = "B", Index = 1, X = 60, Y = 160 },
                new LedPoint() { Diameter = 36, Text = "B02", Group = "B", Index = 2, X = 120, Y = 160 },
                new LedPoint() { Diameter = 36, Text = "B03", Group = "B", Index = 3, X = 180, Y = 160 },
                new LedPoint() { Diameter = 36, Text = "B04", Group = "B", Index = 4, X = 80, Y = 210 },
                new LedPoint() { Diameter = 36, Text = "B05", Group = "B", Index = 5, X = 160, Y = 210 },
                new LedPoint() { Diameter = 36, Text = "B06", Group = "B", Index = 6, X = 260, Y = 210 },
            ],
        },
    };
}
